﻿using Shop.Model.ActivityGoods;
using Shop.Model.DB;
using Shop.RemoteModel.ActivityGoods.Model;
using Shop.Repository;
using WeDonekRpc.Helper;
using WeDonekRpc.Helper.IdGenerator;

namespace Shop.Collect.lmpl
{
    internal class ActivityGoodsCollect : IActivityGoodsCollect
    {
        private readonly IActivityGoodsRepository _Repository;

        public ActivityGoodsCollect (IActivityGoodsRepository repository)
        {
            this._Repository = repository;
        }

        public DBShopActivityGoods[] Sync (long activityId, ActivityGoods[] goods)
        {
            DBShopActivityGoods[] datas = this._Repository.GetsByActivityId<DBShopActivityGoods>(activityId);
            if (datas.IsNull())
            {
                return this._Repository.Adds(activityId, goods);
            }
            DBShopActivityGoods[] adds = goods.Convert(a => !datas.IsExists(c => c.GoodsId == a.GoodsId && c.SkuId == a.SkuId), a => new DBShopActivityGoods
            {
                ActivityId = activityId,
                GoodsId = a.GoodsId,
                SkuId = a.SkuId,
                Id = IdentityHelper.CreateId()
            });
            long[] dropId = datas.Convert(a => !goods.IsExists(c => c.GoodsId == a.GoodsId && c.SkuId == a.SkuId), a => a.Id);
            if (adds.IsNull() && dropId.IsNull())
            {
                return datas;
            }
            int num = datas.Length + adds.Length - dropId.Length;
            this._Repository.Sync(adds, dropId, activityId, num);
            datas = datas.FindAll(c => !dropId.Contains(c.Id));
            if (!adds.IsNull())
            {
                return datas.Add(adds);
            }
            return datas;
        }

        public void Delete (DBShopActivityGoods obj)
        {
            this._Repository.Delete(obj.Id);
        }
        public void Delete (long[] ids)
        {
            this._Repository.Delete(ids);
        }
        public long[] GetSkuIds (long activityId)
        {
            return this._Repository.Gets(a => a.ActivityId == activityId, a => a.SkuId);
        }
        public void SetPrice (Dictionary<long, decimal> price)
        {
            this._Repository.SetPrice(price);
        }
        public bool SetPrice (DBShopActivityGoods obj, decimal price, bool isMust)
        {
            if (obj.ActivityPrice == price && obj.IsMust == isMust)
            {
                return false;
            }
            this._Repository.SetPrice(obj.Id, price, isMust);
            return true;
        }

        public DBShopActivityGoods Get (long id)
        {
            return this._Repository.Get(id);
        }

        public Result[] GetsByActivityId<Result> (long activityId) where Result : class, new()
        {
            return this._Repository.GetsByActivityId<Result>(activityId);
        }
        public bool CheckIsExists (long activityId)
        {
            return this._Repository.IsExist(a => a.ActivityId == activityId);
        }

        public GoodsPitchInActivity[] GetPitchInActivity (DateTime begin, DateTime end, long[] skuId)
        {
            return this._Repository.GetPitchInActivity(begin, end, skuId);
        }

        public void Clear (long activityId)
        {
            long[] ids = this._Repository.Gets(a => a.ActivityId == activityId, a => a.Id);
            if (ids.IsNull())
            {
                return;
            }
            this._Repository.Delete(ids);
        }
    }
}
